package com.eportal.service.impl;

import com.eportal.base.BaseDAO;
import com.eportal.dao.CityEntityMapper;
import com.eportal.domain.City;
import com.eportal.entity.CityEntity;
import com.eportal.service.CityService;
import com.eportal.vo.controller.background.cinstallunit.CInstallUnitSelectListVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 *
 * @author chencheng
 *
 */
@Service("cityService")
public class CityServiceImpl  implements CityService {
	@Resource(name="dao")
	private BaseDAO dao;


    @Resource
    private CityEntityMapper cityDao;



    @Override
    public List<City> getProvices() {
    	return dao.query("FROM City WHERE dm LIKE '%0000' ORDER BY dm");
    }

    @Override
    public List<City> getCitys(String province) {
        // 直辖市
        if (isMunicipal(province)) {
            province = province.substring(0, 2);
            return dao.query("FROM City WHERE isleaf = 1 AND dm  LIKE '" + province + "%' ORDER BY dm ");
        } else {
            province = province.substring(0, 2);
            return dao.query("FROM City WHERE dm LIKE '" + province + "%00' AND dm NOT LIKE '%0000' ORDER BY dm ");
        }
    }

    @Override
    public List<City> regions(String city) {
        if (city == null || city.trim().length() == 0) {
            return null;
        }
        boolean result = isMunicipal(city.substring(0, 2));
        if (result) {
            return null;
        } else {
            city = city.substring(0, 4);
            return dao.query("FROM City WHERE isleaf = 1 AND dm LIKE '" + city + "%' ORDER BY dm ");
        }
    }

    public boolean isMunicipal(String province) {
        int isExist = municipalities.indexOf(province);
        return isExist > -1;
    }

    @Override
    public Object get(String hql) {
		return  dao.loadObject(hql);
	}


//todo ----------------------------------------------------------------------------------------------


    /**
     * 城市下拉列表
     *
     * @param province 省
     * @return
     * @author Saber
     * @Date 2017/8/10 上午10:23
     */
    @Override
    public List<CityEntity> selectCityList(String province) {

        if(StringUtils.isEmpty(province)){
            return null;
        }

        //是否直辖市
        boolean isMunicipal = isMunicipal(province);
        // 直辖市
        if (isMunicipal(province)) {
            province = province.substring(0, 2);
            //return dao.query("FROM City WHERE isleaf = 1 AND dm  LIKE '" + province + "%' ORDER BY dm ");
        } else {
            province = province.substring(0, 2);
            //return dao.query("FROM City WHERE dm LIKE '" + province + "%00' AND dm NOT LIKE '%0000' ORDER BY dm ");
        }
        return cityDao.selectCityList(province,isMunicipal);
    }

    /**
     * 区域下拉列表
     *
     * @param city
     * @return
     */
    @Override
    public List<CityEntity> selectRegionList(String city) {
        if(StringUtils.isEmpty(city)){
            return null;
        }
        boolean result = isMunicipal(city.substring(0, 2));
        if (result) {
            return null;
        } else {
            city = city.substring(0, 4);
            //return dao.query("FROM City WHERE isleaf = 1 AND dm LIKE '" + city + "%' ORDER BY dm ");
        }
        return cityDao.selectRegionList(city);
    }
}
